package mudbconnection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import muobject.Classo;
import muobject.Student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component("DBClassesService")
public class DBClassesService implements DBClasses{
	
	private JdbcTemplate jdbct;

	@Autowired
	public DBClassesService(DataSource jdbct) {
		super();
		this.jdbct = new JdbcTemplate(jdbct);
	}
	
	public List<Classo> getAllClasses(){
		try{
			@SuppressWarnings("unchecked")
			List<Classo> classo = (List<Classo>)jdbct.query("Select * from Classes ", new ClassMapper());
			return classo;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return null;
		}
	}
	/*
	public List<Classo> getSpicalSchoolClasses(String sno){
		try{
			@SuppressWarnings("unchecked")
			List<Classo> classo = (List<Classo>)jdbct.query("Select * from Classes where Sno=?", 
					new Object[]{sno},
					new ClassMapper());
			return classo;
		}catch(Exception e){
			System.out.println(e.getMessage());
			return null;
		}
	}
	*/
	class ClassMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
			Classo classo = null;
			try{
				classo = new Classo(rs.getInt("cid"), rs.getString("sno"), rs.getString("grade")
						,rs.getString("sclass"), rs.getDate("createdate"), rs.getDate("updatedate"));
			}catch(Exception e){
				System.out.println(e.getMessage());
			}
			return classo;
		}
	}
}
